package thinking

object 类型匹配 {
  def main(args: Array[String]): Unit = {

    for (arr <- Array(Array(0), Array(1, 0), Array(0, 1, 0),
      Array(1, 1, 0), Array(1, 1, 0, 1))) {

      val result = arr match {
        case Array(0) => 0
        case Array(x, y) => x + ":" + y
        case Array(0, _*) => "是以0开头的数组"
        case _ => "什么集合都不是"
      }
      println("result = " + result)
    }


    for (list <- List(List(0), List(1), List(0, 0, 0), List(1, 0, 0))) {
      val result = list match {
        case 0 :: Nil => "只有0"
        case 1 :: Nil => "只有1"
        case a :: b :: 0 :: Nil => "类型是 xx0"
        case 1 :: tail => "0开头"
        case _ => "something else"
      }
      println(result)
    }

    for (tuple <- Array((0, 1), (1, 0), (1, 1), (1, 0, 2))) {
      val result = tuple match {
        case (0, _) => "0 ..."
        case (y, 0) => y
        case _ => "other"
      }
      println(result)
    }
  }


}
